﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.BizRule;
using ECCSystem.EntityClass;
using ECCSystem.CheckClass;
using System.Text.RegularExpressions;
//using Microsoft.Office.Core;
using Excel;
namespace ECCSystem
{
	public partial class frmWorkTime : Form
	{
		int intEditMode = 0;
		object objMissing = System.Reflection.Missing.Value;
		public frmWorkTime()
		{
			InitializeComponent();
		}
		

		private void btnSearch_Click(object sender, EventArgs e)
		{
			OpenFileDialog objFileDialog = new OpenFileDialog();
			//string strFileName = "";

			string strFilePath = "";
			objFileDialog.Title = "";
			objFileDialog.Multiselect = false;
			objFileDialog.AddExtension = true;
			objFileDialog.Filter = "Excel Worksheets|*.xls|All files (*.*)|*.*";
			objFileDialog.InitialDirectory = "C:\\";

			if (objFileDialog.ShowDialog(this) == DialogResult.OK)
			{

				strFilePath = objFileDialog.FileName;
				//strFileName = objFileDialog.SafeFileName;

				if (!strFilePath.ToLower().Contains(".xls"))
				{
					MessageBox.Show("ファイルは選択ミスで、導入不可能", "ECC Information");
					return;
				}
				txtFilePath.Text = strFilePath;

				btnImport.Enabled = true;
			}



		}

		private string ImportExcel(string strFilePath)
		{
			Excel.Application app = new Excel.Application();
			Workbook objWorkBook = null;
			Worksheet objWorkSheet = null;
			ClsWorkTime objClsWorkTime = new ClsWorkTime();
			try
			{
				objWorkBook = app.Workbooks.Open(strFilePath, objMissing, objMissing, objMissing, objMissing, objMissing,
                                            objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);


				ArrayList objArrAll = new ArrayList();
				ArrayList objArrCol = new ArrayList();
				int intRows = 4;
				int intCols = 4;
				objWorkSheet = (Worksheet)objWorkBook.Sheets[1];
				if (objWorkSheet.Name != "シフト")
				{
					return "1";
				}
				if (clsUICheck.CheckDate(objWorkSheet.get_Range(objWorkSheet.Cells[2, 4], objWorkSheet.Cells[2, 4]).Value2.ToString()) != true)
				{
					return "1";
				}
				DateTime objDateTime = Convert.ToDateTime(objWorkSheet.get_Range(objWorkSheet.Cells[2, "D"], objWorkSheet.Cells[2, "D"]).Value2);
				int intCount = 0;
				intCount = objClsWorkTime.GetList(objDateTime);
				if (intCount > 0)
				{
					if (MessageBox.Show("導入されたデータを更新しますか？", "ECC Information", MessageBoxButtons.YesNo) != DialogResult.Yes)
					{
						return "1";
					}
					else
					{
						objClsWorkTime.DelWorkTime(objDateTime);
					}
				}
				int i = 4;
				while (clsUICheck.NullToString(objWorkSheet.get_Range(objWorkSheet.Cells[i, "A"], objWorkSheet.Cells[i, "A"]).Value2) != "")
				{
					i++;
				}

				intCount = (i - 4) * objDateTime.AddMonths(1).AddDays(0 - objDateTime.Day).Day;

				pgbTime.Maximum = intCount + 2;
				pgbTime.Minimum = 0;
				pgbTime.Value = 0;
				pgbTime.Visible = true;
				while (clsUICheck.CheckDate(clsUICheck.NullToString(objWorkSheet.get_Range(objWorkSheet.Cells[2, intCols], objWorkSheet.Cells[2, intCols]).Value2)) == true)
				{
					intRows = 4;
					objArrCol = null;
					objArrCol = new ArrayList();
					while (clsUICheck.NullToString(objWorkSheet.get_Range(objWorkSheet.Cells[intRows, "A"], objWorkSheet.Cells[intRows, "A"]).Value2).Trim() != "")
					{
						WorkTimeEntity objWorkTimeEntity = new WorkTimeEntity();
						objWorkTimeEntity.Name = clsUICheck.NullToString(objWorkSheet.get_Range(objWorkSheet.Cells[intRows, "B"], objWorkSheet.Cells[intRows, "B"]).Value2);

						objWorkTimeEntity.WorkTime_Date = Convert.ToDateTime(objWorkSheet.get_Range(objWorkSheet.Cells[2, intCols], objWorkSheet.Cells[2, intCols]).Value2.ToString());
						objWorkTimeEntity.WorkTime_Status = clsUICheck.NullToString(objWorkSheet.get_Range(objWorkSheet.Cells[intRows, intCols], objWorkSheet.Cells[intRows, intCols]).Value2);
						objArrCol.Add(objWorkTimeEntity);
						intRows++;
						pgbTime.Value = pgbTime.Value + 1;
					}
					objArrAll.Add(objArrCol);
					intCols++;
				}
				
				return objClsWorkTime.AddEmailTemplet(objArrAll);
			}
			catch (Exception ex)
			{
				return ex.ToString();
			}
			finally
			{
				objWorkSheet = null;
				objWorkBook.Close(objMissing, objMissing, objMissing);
				app = null;
				System.GC.Collect();
			}
		}

		private void frmWorkTime_FormClosed(object sender, FormClosedEventArgs e)
		{
			Form objForm = new frmMain();
			objForm.Show();
		}

		private void btnImport_Click(object sender, EventArgs e)
		{
			string strFlg = ImportExcel(txtFilePath.Text.Trim());
			pgbTime.Value = pgbTime.Maximum - 1;
			if (strFlg == "")
			{
				MessageBox.Show("データ導入成功！", "ECC Information");

			}
			else if (strFlg == "1")
			{
				MessageBox.Show("データを読み取る時、導入不可能", "ECC Information");
			}
			else
			{
				MessageBox.Show(strFlg, "ECC Catch Error");
			}
			pgbTime.Value = pgbTime.Maximum;
			pgbTime.Visible = false;
		}

		private void btnCannel_Click(object sender, EventArgs e)
		{
			txtFilePath.Text = "";
			btnImport.Enabled = false;
		}

		private void frmWorkTime_Load(object sender, EventArgs e)
		{
			btnImport.Enabled = false;
		}

		
	}
}